package com.trendplus.unitconverter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class CapacityCalculate extends Activity {
	
	Spinner lengthType=null; 
	TextView mTextView;
	
	/** Called when the activity is first created. */
	//foot:英呎
    static final String[] capacityTypeList=new String[] { 
        "毫升", "公升", "公合", "加侖"
    };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.calc_capacity);
	    findViews();
	    touchSpinnerS();
	    setListeners();
	}
	
	private EditText inputValue;
	private Button button_calc;
	private Button button_cleanResult;
	
	public void findViews(){
		inputValue = (EditText) findViewById(R.id.inputValue);
		button_calc = (Button) findViewById(R.id.submit);
		button_cleanResult = (Button) findViewById(R.id.cleanData);
	}
	//Listen for button clicks
    private void setListeners() {
        button_calc.setOnClickListener(calcCapacity);
        button_cleanResult.setOnClickListener(cleanEditText);
    }
    
    public void touchSpinnerS(){
		Spinner SpinnerS = (Spinner)findViewById(R.id.mySpinner);

        //設定功能表項目陣列，使用createFromResource()
        //ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.items, android.R.layout.simple_spinner_item);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, capacityTypeList);

        //設定選單
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mTextView = (TextView) findViewById(R.id.textViewCapacity);
        //設定adapter
        SpinnerS.setAdapter(adapter);
        
        SpinnerS.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                mTextView.setText(capacityTypeList[arg2]);
                arg0.setVisibility(View.VISIBLE);
            }
 
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
 
        });
	}
    
    private OnClickListener calcCapacity = new OnClickListener(){
		public void onClick(View v) {
			TextView capacityType = (TextView)findViewById(R.id.textViewCapacity);
			EditText inputValue = (EditText)findViewById(R.id.inputValue);
			String capacityTypes = capacityType.getText().toString();
			long inputValues = Long.parseLong((inputValue.getText().toString()));
			System.out.println("capacityType="+capacityTypes+", values="+inputValues);
			
			converterOfCapacity(capacityTypes,inputValues);
		}
    };
    
    public void converterOfCapacity(String capacityTypes, double value){
    	TextView milliliterResult = (TextView)findViewById(R.id.milliliterResult);
    	TextView literResult = (TextView)findViewById(R.id.literResult);
    	TextView deciliterResult = (TextView)findViewById(R.id.deciliterResult);
    	TextView gallonResult = (TextView)findViewById(R.id.gallonResult);
    	
    	if(capacityTypes.equalsIgnoreCase("毫升")){
    		String literStr = Double.toString(value*0.001);
    		String deciliterStr = Double.toString(value*0.01);
    		String gallonStr = Double.toString(value*0.00026);
    		
    		milliliterResult.setText("="+Double.toString(value)+"毫升");
    		literResult.setText("="+literStr+"公升");
    		deciliterResult.setText("="+deciliterStr+"公合");
    		gallonResult.setText("="+gallonStr+"加侖");
    	}else if(capacityTypes.equalsIgnoreCase("公升")){
    		String milliliterStr = Double.toString(value*1000);
    		String deciliterStr = Double.toString(value*10);
    		String gallonStr = Double.toString(value*0.26418);
    		
    		milliliterResult.setText("="+milliliterStr+"毫升");
    		literResult.setText("="+Double.toString(value)+"公升");
    		deciliterResult.setText("="+deciliterStr+"公合");
    		gallonResult.setText("="+gallonStr+"加侖");
    	}else if(capacityTypes.equalsIgnoreCase("公合")){
    		String milliliterStr = Double.toString(value*100);
    		String literStr = Double.toString(value*10);
    		String gallonStr = Double.toString(value*0.026418);
    		
    		milliliterResult.setText("="+milliliterStr+"毫升");
    		literResult.setText("="+literStr+"公升");
    		deciliterResult.setText("="+Double.toString(value)+"公合");
    		gallonResult.setText("="+gallonStr+"加侖");
    	}else if(capacityTypes.equalsIgnoreCase("加侖")){
    		String milliliterStr = Double.toString(value*3785.33);
    		String literStr = Double.toString(value*3.78533);
    		String deciliter = Double.toString(value*37.8533);
    		
    		milliliterResult.setText("="+milliliterStr+"毫升");
    		literResult.setText("="+literStr+"公升");
    		deciliterResult.setText("="+deciliter+"公合");
    		gallonResult.setText("="+Double.toString(value)+"加侖");
    	}
    	
    }
    
    private OnClickListener cleanEditText = new OnClickListener(){

		public void onClick(View v) {
			EditText inputValue = (EditText)findViewById(R.id.inputValue);
			TextView milliliterResult = (TextView)findViewById(R.id.milliliterResult);
	    	TextView literResult = (TextView)findViewById(R.id.literResult);
	    	TextView deciliterResult = (TextView)findViewById(R.id.deciliterResult);
	    	TextView gallonResult = (TextView)findViewById(R.id.gallonResult);
			inputValue.setText("");
			milliliterResult.setText("");
    		literResult.setText("");
    		deciliterResult.setText("");
    		gallonResult.setText("");
		}
    };
    
    public void resultSetting(){
    	
    }
    
    
}
